package com.szbmyoubao.app.view.dialog

import android.app.Activity
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.ellecity06.common.R
import com.ellecity06.common.base.BaseBottomDialogFragment
import com.ellecity06.common.databinding.DialogImageSelectorBinding

/**
 * @author ellecity06
 * @time 2024/10/28 18:12
 * @des
 */

fun FragmentActivity.showBottomSelect(
    textArr: Array<String> = arrayOf("录音", "从相册中获取"),
    onCamera: (() -> Unit)? = null,
    onGallery: (() -> Unit)? = null
) {
    BottomSelectDialog(this, textArr, onCamera, onGallery).show(supportFragmentManager,"BottomSelectDialog")
}

fun Fragment.showBottomSelect(
    textArr: Array<String> = arrayOf("录音", "从相册中获取"),
    onCamera: (() -> Unit)? = null,
    onGallery: (() -> Unit)? = null
) {
    BottomSelectDialog(activity = requireActivity(), textArr, onCamera, onGallery).show(childFragmentManager,"BottomSelectDialog")
}

 class BottomSelectDialog(
    val activity: Activity,
    val textArr: Array<String> = arrayOf("录音", "从相册中获取"),
    private val onCamera: (() -> Unit)? = null,
    private val onGallery: (() -> Unit)? = null
) :
    BaseBottomDialogFragment<DialogImageSelectorBinding>(), View.OnClickListener {
//    override fun inflaterViewBinding(): DialogImageSelectorBinding {
//        return DialogImageSelectorBinding.inflate(LayoutInflater.from(activity))
//    }

    override val animation: Int
        get() = R.style.Animation_Design_BottomSheetDialog

    override fun inflateBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): DialogImageSelectorBinding {
        return DialogImageSelectorBinding.inflate(inflater, container, false)
    }

    override val gravity: Int
        get() = Gravity.BOTTOM

    override fun initialize() {
        if (textArr.isNotEmpty()) {
            binding.tvTakePhoto.text = textArr[0]
        }
        if (textArr.size > 1) {
            binding.tvFromGallery.text = textArr[1]
        }
        binding.tvCancel.setOnClickListener(this)
        binding.tvFromGallery.setOnClickListener(this)
        binding.tvTakePhoto.setOnClickListener(this)
    }

    override fun onClick(view: View?) {
        when (view?.id) {
            binding.tvCancel.id -> {
                dismiss()

            }

            binding.tvFromGallery.id -> {
                dismiss()
                onGallery?.invoke()
            }

            binding.tvTakePhoto.id -> {
                dismiss()
                onCamera?.invoke()
            }
        }
    }
}